// pages/records/records.js
Page({
  data: {
    records: {
      totalTimes: 0,
      correctCount: 0,
      correctRate: 0
    },
    challengeData: {
      currentLevel: 1,
      levelStars: 0
    },
    categoryStats: {
      letters: 0,
      words: 0,
      spelling: 0,
      listening: 0,
      challenge: 0
    },
    challengeProgress: 0
  },

  onLoad() {
    this.loadRecords();
  },

  onShow() {
    // 每次显示页面时重新加载记录
    this.loadRecords();
  },

  // 加载记录
  loadRecords() {
    // 加载学习记录
    const records = wx.getStorageSync('learningRecords') || {
      totalTimes: 0,
      correctCount: 0,
      correctRate: 0
    };

    // 加载闯关数据
    const challengeData = wx.getStorageSync('challengeData') || {
      currentLevel: 1,
      levelStars: 0
    };

    // 加载分类统计
    const categoryStats = wx.getStorageSync('categoryStats') || {
      letters: 0,
      words: 0,
      spelling: 0,
      listening: 0,
      challenge: 0
    };

    // 计算闯关进度（假设每10关为一个阶段）
    const challengeProgress = ((challengeData.currentLevel - 1) % 10) * 10;

    this.setData({
      records,
      challengeData,
      categoryStats,
      challengeProgress
    });
  },

  // 重置记录
  resetRecords() {
    wx.showModal({
      title: '确认重置',
      content: '确定要重置所有学习记录吗？此操作不可恢复！',
      success: (res) => {
        if (res.confirm) {
          // 重置学习记录
          wx.setStorageSync('learningRecords', {
            totalTimes: 0,
            correctCount: 0,
            correctRate: 0
          });

          // 重置闯关数据
          wx.setStorageSync('challengeData', {
            currentLevel: 1,
            levelStars: 0
          });

          // 重置分类统计
          wx.setStorageSync('categoryStats', {
            letters: 0,
            words: 0,
            spelling: 0,
            listening: 0,
            challenge: 0
          });

          // 重新加载
          this.loadRecords();

          wx.showToast({
            title: '记录已重置',
            icon: 'success'
          });
        }
      }
    });
  }
});

